package com.yidu.ruifeng_logistics.service.impl;

import com.yidu.ruifeng_logistics.dao.*;
import com.yidu.ruifeng_logistics.entity.*;
import com.yidu.ruifeng_logistics.service.LogisticsPredictionService;
import com.yidu.ruifeng_logistics.vo.LogisMoneyVo;
import com.yidu.ruifeng_logistics.vo.LogisticsPredictionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 下单表(LogisticsPrediction)表服务实现类
 *
 * @author 文湘
 * @since 2021-03-09 14:29:00
 */
@Service("logisticsPredictionService")
public class LogisticsPredictionServiceImpl implements LogisticsPredictionService {
    @Resource
    private LogisticsPredictionDao logisticsPredictionDao;
    /**
     * 地区
     */
    @Resource
    private LogisticsAreaDao logisticsAreaDao;
    /**
     * 计算价格
     */
    @Resource
    private LogisticsPriceDao logisticsPriceDao;


    @Resource
    private LogisticsCargoDao logisticsCargoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param predictionId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsPrediction queryById(Integer predictionId) {
        return this.logisticsPredictionDao.queryById(predictionId);
    }

    /**
     * 查询多条数据
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsPrediction> queryAllByLimit(int offset, int limit) {
        return this.logisticsPredictionDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<LogisticsPredictionVo> selectGuanLian() {
        return this.logisticsPredictionDao.selectGuanLian();
    }

    /**
     * 通过主键删除数据
     *
     * @param predictionId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer predictionId) {
        return this.logisticsPredictionDao.deleteById(predictionId) > 0;
    }

    @Override
    public List<LogisticsPrediction> queryAll(LogisticsPrediction logisticsPrediction) {
        return this.logisticsPredictionDao.queryAll(logisticsPrediction);
    }

    @Override
    public LogisticsPredictionVo selectUpdateId(Integer predictionId) {
        return logisticsPredictionDao.selectUpdateId(predictionId);
    }


    /**
     * 通过出发地 到 目的地  和重量
     * 返回价格
     * @param moneyVo
     * @return
     */
    @Override
    public Double selectMoney(LogisMoneyVo moneyVo) {
            Double money = null;
            LogisticsArea chufa = null;
            LogisticsArea mudi = null;
            List<LogisticsPrice> logisticsPrices=null;
            //  如果大于100 则说明是市级id 需要去查询父Id
            if (Integer.valueOf(moneyVo.getPredictionDeparture())>100){
                //  出发地的父ID
                chufa = logisticsAreaDao.queryById(Integer.valueOf(moneyVo.getPredictionDeparture()));
            }
            if (Integer.valueOf(moneyVo.getPredictionBourn())>100){
                //  目的地的父ID
                mudi = logisticsAreaDao.queryById(Integer.valueOf(moneyVo.getPredictionBourn()));
            }
            //  返回价格区间对象
            //  当出发地大于目的地 就将他们反过来
            if (chufa.getAreaUp()>mudi.getAreaUp()){
                 logisticsPrices = logisticsPriceDao.selectMoney(mudi.getAreaUp(), chufa.getAreaUp());
            }else {
                logisticsPrices = logisticsPriceDao.selectMoney(chufa.getAreaUp(), mudi.getAreaUp());
            }
            // 货物的重量
            Double cargoWeight = Double.valueOf(moneyVo.getCargoWeight());
            for (LogisticsPrice price:logisticsPrices){
                //  取出价格区间  比较
                String priceWeight = price.getPriceWeight();
                String[] split = priceWeight.split("~");
                Integer yi = Integer.valueOf(split[0]);
                Integer er = Integer.valueOf(split[1]);
                if (cargoWeight>=yi&&cargoWeight<=er){
                    String qujian = yi+"~"+er;
                    if (qujian.equals(priceWeight)){
                        money= price.getPriceMoney();
                    }
                }
            }
            return money;
        }

    @Override
    public List<LogisticsPredictionVo> selectByIdClient(Integer clientId) {
        return logisticsPredictionDao.selectByIdClient(clientId);
    }

    @Override
    public List<LogisticsPredictionVo> selectAreaStaffId(String depaArea) {
        return logisticsPredictionDao.selectAreaStaffId(depaArea);
    }


    /**
     * 新增数据
     * @param logisticsPredictionVo 实例对象
     * @return
     */
    @Override
    @Transactional  // 加入事务
    public LogisticsPrediction insert(LogisticsPredictionVo logisticsPredictionVo,
                                      HttpSession session) {
        LogisticsPrediction prediction =null;
        Object obj = session.getAttribute("identity");
                //  先取出页面登录进来客户的ID
        if (obj!=null && obj!=""){
            if ("client".equals(obj)){
                LogisticsClient clients = (LogisticsClient)session.getAttribute("login");
                clients.getClientId();
                System.out.println(clients.getClientId()+"++++++++++++++++++++++++++");
        //  先生成一个货物表  然后拿到新生成的货物订单
        LogisticsCargo cargo = new LogisticsCargo();
        cargo.setCargoType(logisticsPredictionVo.getCargoType());
        cargo.setCargoWeight(Double.valueOf(logisticsPredictionVo.getCargoWeight()));
        cargo.setCargoExplain(logisticsPredictionVo.getCargoExplain());
        logisticsCargoDao.insert(cargo);
        System.out.println("新增货物表返回的ID+++++++++++++++++"+cargo.getCargoId());
        // 判断新增货物表返回的ID是否位空
        if (cargo.getCargoId()!=null){
            prediction = new LogisticsPrediction();
            prediction.setCargoId(cargo.getCargoId()); // 货物表ID
            prediction.setClientId(clients.getClientId());  // 客户ID
            prediction.setPredictionBourn(logisticsPredictionVo.getPredictionBourn()); // 目的地
            prediction.setPredictionDeparture(logisticsPredictionVo.getPredictionDeparture()); // 出发地
            prediction.setPredictionBusiness(logisticsPredictionVo.getPredictionBusiness()); // 业务类型
            prediction.setPredictionMoney(Double.valueOf(logisticsPredictionVo.getPredictionMoney())); // 价格
            prediction.setPredictionRecipient(logisticsPredictionVo.getXinClientId()); // 取件人
            prediction.setPredictionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));  //下单时间
            logisticsPredictionDao.insert(prediction);
        }
            }else if ("staff".equals(obj)){
                return prediction;
            }
        }
        return prediction;
    }


    /**
     * 修改数据
     * @param logisticsPredictionVo 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional // 加入事务
    public LogisticsPrediction update(LogisticsPredictionVo logisticsPredictionVo) {
        LogisticsPrediction prediction =null;
            //  先修改货物表  然后拿到新生成的货物订单
            LogisticsCargo cargo = new LogisticsCargo();
            cargo.setCargoId(logisticsPredictionVo.getCargoId());
            cargo.setCargoType(logisticsPredictionVo.getCargoType());
            cargo.setCargoWeight(Double.valueOf(logisticsPredictionVo.getCargoWeight()));
            cargo.setCargoExplain(logisticsPredictionVo.getCargoExplain());
                logisticsCargoDao.update(cargo);
                prediction = new LogisticsPrediction();
                prediction.setPredictionId(logisticsPredictionVo.getPredictionId()); // 自己表的ID
                prediction.setCargoId(logisticsPredictionVo.getCargoId()); // 货物表ID
                prediction.setClientId(logisticsPredictionVo.getClientId());  // 客户ID
                prediction.setPredictionBourn(logisticsPredictionVo.getPredictionBourn()); // 目的地
                prediction.setPredictionDeparture(logisticsPredictionVo.getPredictionDeparture()); // 出发地
                prediction.setPredictionBusiness(logisticsPredictionVo.getPredictionBusiness()); // 业务类型
                prediction.setPredictionMoney(Double.valueOf(logisticsPredictionVo.getPredictionMoney())); // 价格
                prediction.setPredictionRecipient(logisticsPredictionVo.getXinClientId()); // 取件人
                prediction.setPredictionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));  //下单时间
                logisticsPredictionDao.update(prediction);
        return prediction;
    }

}

